package com.codeduck.service.producer;

import com.alibaba.fastjson.JSON;
import com.codeduck.kafka.KafkaInvoker;
import com.codeduck.pojo.AcctDetail;
import com.codeduck.pojo.AcctInfo;
import com.codeduck.pojo.TopicHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @auther Mahone Yan
 * @date 2023/7/16
 */
@Component
public class AcctDetailProducer {

    private static final Logger LOGGER = LoggerFactory.getLogger(AcctDetailProducer.class);

    @Resource
    private KafkaInvoker kafkaInvoker;

    @Scheduled(cron = "0/10 * * * * ?")
    public void job() {
        this.doJob(0);
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void job1() {
        this.doJob(1);
    }

    private void doJob(int partition) {
        AcctDetail acctDetail = this.getAcctDetail(partition);
        try {
            kafkaInvoker.send(TopicHandler.ACCT_DETAIL_TOPIC.getTopic(), partition, null, JSON.toJSONString(acctDetail));
            LOGGER.info("============> 交易明细消息已发送至 Topic[{}], partition[{}] <============", TopicHandler.ACCT_DETAIL_TOPIC.getTopic(), partition);
        } catch (Exception e) {
            LOGGER.error("交易明细定时任务向Kafka发送消息时出现异常", e);
        }
    }

    private AcctDetail getAcctDetail(int var) {
        Random random = new Random();
        AcctDetail acctDetail = new AcctDetail();
        AcctInfo acctInfo = new AcctInfo();
        acctInfo.setAcctName(var + "张三" + random.nextInt(100));
        acctInfo.setAcct("99966315261111111");
        acctInfo.setBalance(random.nextLong());
        acctDetail.setAcctInfo(acctInfo);
        acctDetail.setRecAcct("8886666611111111");
        acctDetail.setAmount(random.nextLong());
        acctDetail.setRecAcctName("李四" + random.nextInt(100));
        return acctDetail;
    }
}
